package fischl.douyuApi;

import fischl.Platform;
import fischl.RoomLoader;
import fischl.VideoMarker;
import fischl.douyuApi.model.Danmu;
import fischl.douyuApi.model.Video;
import fischl.douyuApi.model.VideoDateData;
import fischl.douyuApi.model.VideoP;
import fischl.log.Log;
import fischl.log.LogFactory;
import fischl.model.VideoModel;
import fischl.model.Room;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class DouyuWatcher extends Platform {
  private static final Log log = LogFactory.get("watcher");

  private List<VideoModel> videos = new ArrayList<>();

  /**
   * 3分钟检查一次
   */
  public void doWatch() {
    List<VideoModel> result = new ArrayList<>();

    List<Room> rooms = RoomLoader.load();
    for (Room room : rooms) {
      final String upId = room.upId;
      final VideoP p = DouyuService.getLastVideo(upId);
      if (p == null) continue;
      if (!"今天".equals(p.date_format) && !"昨天".equals(p.date_format)) continue;
      final Video video = p.video_list.get(0);
      final VideoDateData data = DouyuService.getVideosByP(video.hash_id, upId);
      if (data == null) continue;

      String publishTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());

      List<VideoModel> mapped = data.list.stream().map(v -> {
        VideoModel model = new VideoModel();
        model.upId = upId;
        model.vid = v.hash_id;
        model.title = v.nickname + "【" + data.date + " " + v.show_remark + "】";
        model.cover = v.cover;
        model.upName = v.nickname;
        try {
          model.dateStr = v.title.substring(1, v.title.indexOf(' '));
        } catch (Exception e) {
          log.e(e);
        }
        model.liveRoom = room.liveRoom;
        model.videoUrl = "https://v.douyu.com/show/" + v.hash_id;
        model.publishTime = publishTime;
        model.sliceCount = data.list.size();
        try {
          String[] split = data.date.split("-");
          Date date = new Date();
          date.setYear(Integer.parseInt(split[0]) - 1900);
          date.setMonth(Integer.parseInt(split[1]) - 1);
          date.setDate(Integer.parseInt(split[2]));
          date.setHours(Integer.parseInt(v.show_remark.substring(0, 2)));
          date.setMinutes(0);
          date.setSeconds(0);
          model.date = date;
        } catch (Exception e) {
          log.e(e);
        }
        return model;
      }).collect(Collectors.toList());
      result.addAll(mapped);
    }

    Set<String> ids = VideoMarker.getAllMarkedIds();
    result.stream()
      .sorted((o1, o2) -> o2.date.compareTo(o1.date))
      .limit(40)
      .forEach(v -> {
        v.isProcessed = ids.contains(v.vid);
        videos.add(v);
      });
  }

  public List<VideoModel> getSnapshot() {
    return videos;
  }

  public VideoModel getVideoModelByVid(String vid) {
    return getSnapshot().stream().filter(videoModel -> {
      return vid.equals(videoModel.vid);
    }).findAny().orElse(null);
  }

  @Override
  public List<Danmu> getDanmu(String vid) {
    return DouyuService.getDanmuByHashId(vid);
  }

}
